namespace SimpleWebServices.Models
{
    using System.Xml.XPath;
    using SimpleWebServices;
    using Xunit;

    public sealed class TelephoneFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<Telephone>()
                .DerivesFrom<object>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .XmlRoot("telephone")
                .Result);
        }

        [Fact]
        public void deserialize()
        {
            Telephone actual = "<telephone normalized='+442078085900' number='(0207) 808 5900' key='work' />".Deserialize<Telephone>();

            Assert.Equal<string>("+442078085900", actual.Normalized);
            Assert.Equal<string>("(0207) 808 5900", actual.Number);
            Assert.Equal<string>("work", actual.Key);
        }

        [Fact]
        public void deserializeEmpty()
        {
            Telephone actual = "<telephone />".Deserialize<Telephone>();

            Assert.Null(actual.Number);
            Assert.Null(actual.Normalized);
            Assert.Null(actual.Key);
        }

        [Fact]
        public void serialize()
        {
            Telephone obj = new Telephone
            {
                Normalized = "+442078085900",
                Number = "(0207) 808 5900",
                Key = "work"
            };

            XPathNavigator navigator = obj.Serialize().CreateNavigator();

            Assert.True((bool)navigator.Evaluate("1=count(/telephone[@key='work'][@normalized='+442078085900'][@number='(0207) 808 5900'])"));
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new Telephone());
        }

        [Fact]
        public void prop_Key()
        {
            Assert.True(new PropertyExpectations(typeof(Telephone).GetProperty("Key"))
                .XmlAttribute("key")
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_Normalized()
        {
            Assert.True(new PropertyExpectations(typeof(Telephone).GetProperty("Normalized"))
                .XmlAttribute("normalized")
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void prop_Number()
        {
            Assert.True(new PropertyExpectations(typeof(Telephone).GetProperty("Number"))
                .XmlAttribute("number")
                .IsAutoProperty<string>()
                .Result);
        }

        [Fact]
        public void op_Normalize_string()
        {
            string expected = "+442078085900";
            string actual = Telephone.Normalize(44, "(0207) 808 5900");

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void op_Normalize_stringEmpty()
        {
            string expected = string.Empty;
            string actual = Telephone.Normalize(44, string.Empty);

            Assert.Equal<string>(expected, actual);
        }

        [Fact]
        public void op_Normalize_stringNull()
        {
            Assert.Null(Telephone.Normalize(44, null as string));
        }
    }
}